package top.asteam.springcloud.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Author Rock Wang
 * @Time 2021/3/14
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> implements Serializable {

    private Integer code;
    private String message;
    private boolean success;
    private T data;

    public CommonResult(Integer code, String message, boolean success) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = null;
    }

    public CommonResult setMessage(final String message){
        this.message=message;
        return this;
    }

    /**
     * 操作成功
     *
     * @return
     */
    public static CommonResult Ok() {
        CommonResult r = new CommonResult();
        r.setData(null);
        r.setSuccess(true);
        r.setCode(200);
        r.setMessage("操作成功");
        return r;
    }

    /**
     * 操作成功并携带数据
     * @param data
     * @return
     */
    public static<T> CommonResult<T> OkData(T data) {
        CommonResult<T> r = new CommonResult<T>();
        r.setData(data);
        r.setSuccess(true);
        r.setCode(200);
        r.setMessage("操作成功");
        return r;
    }

    /**
     * 服务器端操作失败
     *
     * @return
     */
    public static CommonResult serverError(String msg) {
        CommonResult r = new CommonResult();
        r.setData(null);
        r.setSuccess(false);
        r.setCode(500);
        r.setMessage(msg);
        return r;
    }
}
